home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1713 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: is this string a number?
  5. Date: Tue, 16 Jan 1996 12:48:49 GMT
  6. Organization: Netcom
  7. Message-ID: <30fb9bf7.36131328@nntp.ix.netcom.com>
  8. References: <4dbogk$763@jupiter.planet.net> <30f9ac87.195826304@nntp.ix.netcom.com> <DL9tzD.H0@uns.bris.ac.uk>
  9. NNTP-Posting-Host: ix-dc6-24.ix.netcom.com
  10. X-NETCOM-Date: Tue Jan 16  4:48:41 AM PST 1996
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. nathan@pact.srf.ac.uk (Nathan Sidwell) wrote:
  14.  
  15. |>Mike Rubenstein (miker3@ix.netcom.com) wrote:
  16. |>: Chris Kemp <chrisk@paladn.com> wrote:
  17. |>
  18. |>: |>I am inputting a string from the keyboard from a user, and 
  19. |>: |>intend to use the atol or strtol functions to convert the 
  20. |>: |>string to a number.
  21. |>
  22. |>: Use strtol() and supply a non-null second argument.  When strtol()
  23. |>: returns, the pointer will point to the first character that was
  24. not
  25. |>: converted.  Example:
  26. |>
  27. |>:     #include <stdlib.h>
  28. |>
  29. |>:     char *str;
  30. |>:     char *nstr;
  31. |>:     long l;
  32. |>
  33. |>:     /* ... */
  34. |>
  35. |>:     l = strtol(str, &nstr, 10);
  36. |>:     if (nstr == str || *nstr !=  '\0')
  37. |>:     {
  38. |>:       /* str was empty or there is an illegal character */
  39. |>:     }
  40. |>
  41. |>strtol et al remove leading white space, thus if the string " " is
  42. supplied
  43. |>the above check won't tell if the string was blank (nstr and str
  44. will
  45. |>be different and *nstr will be 0). Depending on
  46. |>your application this may or may not be a problem.
  47.  
  48. Was it too much trouble to read the manual to find out what strtol()
  49. does?
  50.  
  51. The problem you cite is not a problem because that's just not how
  52. strotol() works.  From ISO 7.10.1.5:
  53.  
  54.     #include <stdlib.h>
  55.     long int strtol(const char *nptr, char **endptr, int base);
  56.  
  57.     ...
  58.  
  59.     The subject sequence is defined as the longest initial 
  60.     subsequence of the input string, starting with the first 
  61.     non-white-space character character, that is of the expected 
  62.     form.  The subject sequence contains no characters if the 
  63.     input string is empty or consists entirely of white space, or 
  64.     if the first non-white-space character is other than a sign
  65.     or a permissible letter or digit.
  66.  
  67.     ...
  68.  
  69.     If the subject sequence is empty or does not have the expected
  70.  
  71.     form, no conversion is performed; the value of nptr is stored 
  72.     in the object ointed to by endptr, provided that endptr is not
  73.  
  74.     a null character.
  75.  
  76.  
  77. Michael M Rubenstein
  78.